package com.shundr.shipper.frame.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.shundr.shipper.common.model.HistoryAreaInfo;
import com.shundr.shipper.frame.util.LLoader;
import com.shundr.shipper.frame.util.LLogger;

/**
 * 
 * DBHelper 数据库管理类
 * 
 * @author larry 2015-4-10 下午2:47:02
 * @version 1.0.0
 * 
 */
public class DBHelper extends OrmLiteSqliteOpenHelper {

	public static final String DATABASE_NAME = "shundr_db";
	/**
	 * 因为1.0版本数据库为10,所以2.0数据库版本从11开始
	 * 11(基础数据库包括11)之前四张表,用户表、货源表、区域表、基础数据表、路线表
	 */
	public static int DATABASE_VERSION = 13;//第一个版本为12，第二个版本为13
	private Context mContext;

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.mContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase database,ConnectionSource connectionSource) {}

	@Override
	public void onUpgrade(SQLiteDatabase database,
			ConnectionSource connectionSource, int oldVer, int newVer) {
		try {
			LLogger.e("larry","from version " + oldVer + " to new " + newVer);
			if(oldVer < 11){
				LLoader.changeDB(mContext);
			}else if(oldVer == 11){
				
			}else if(oldVer > 11){
				
			}
			
			if(oldVer <= 12){
				TableUtils.dropTable(connectionSource, HistoryAreaInfo.class, true);
				TableUtils.createTableIfNotExists(connectionSource, HistoryAreaInfo.class);
				updateAreasEqual(database);
			}else if(oldVer > 12){
			}
		} catch (Exception e) {
			LLogger.e("Unable to upgrade database from version " + oldVer
					+ " to new " + newVer + " \n " + e);
		}
	}

	@Override
	public void onDowngrade(SQLiteDatabase database, int oldVersion, int newVersion) {
		super.onDowngrade(database, oldVersion, newVersion);
	}
	
	/**
	 * Upgrade tables. In this method, the sequence is:
	 * <b>
	 * <p>[1] Rename the specified table as a temporary table.
	 * <p>[2] Create a new table which name is the specified name.
	 * <p>[3] Insert data into the new created table, data from the temporary table.
	 * <p>[4] Drop the temporary table.
	 * </b>
	 *
	 * @param db The database.
	 * @param tableName The table name.
	 * @param columns The columns range, format is "ColA, ColB, ColC, ... ColN";
	 */
//	private void upgradeTables(SQLiteDatabase db,ConnectionSource connectionSource, String tableName) {
//		try {
//			db.beginTransaction();
//			// 1, Rename table.
//			String tempTableName = tableName + "_temp";
//			String sql = "ALTER TABLE " + tableName + " RENAME TO "
//					+ tempTableName;
//			db.execSQL(sql, null);
//			// 2, Create table.
//			TableUtils.createTableIfNotExists(connectionSource, tableName.getClass());
//			// 3, Load data
//			sql = "INSERT INTO " + tableName + " SELECT * FROM " + tempTableName;
//			db.execSQL(sql, null);
//			// 4, Drop the temporary table.
//			sql = "DROP TABLE IF EXISTS " + tempTableName;
//			db.execSQL(sql, null);
//			db.setTransactionSuccessful();
//		}catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			db.endTransaction();
//		}
//	}
	
	private void updateAreasEqual(SQLiteDatabase db) {
		try {
			db.beginTransaction();
			String sql = "delete from AreaInfo where parentCode = code";
			db.execSQL(sql);
			db.setTransactionSuccessful();
		}catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.endTransaction();
		}
	}

}
